<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/11
 * Time: 17:52
 */

namespace app\controllers;

use app\service\PayService;
use yii;
use app\models\Bank_bill;
use app\models\Department;
use app\models\Staff;

class PayController extends ApiBaseController
{
    public function actionList()
    {
        $p = is_numeric(yii::$app->request->get('p')) ? yii::$app->request->get('p') : 1;
        $s = is_numeric(yii::$app->request->get('s')) ? yii::$app->request->get('s') : 15;
        $where['begin_time'] = yii::$app->request->get('begin_time');
        $where['end_time'] = yii::$app->request->get('end_time');
        $where['invocie'] = trim(yii::$app->request->get('invocie_num'));
        $where['number'] = trim(yii::$app->request->get('number'));
        $where['customer_name'] = trim(yii::$app->request->get('customer_name'));
        $where['service_name'] = trim(yii::$app->request->get('service_name'));
        $where['money'] = trim(yii::$app->request->get('money'));
        $where['type'] = yii::$app->request->get('type');
        $where['type'] = !empty($where['type']) ? explode(',', $where['type']) : $where['type'];
        $where['product'] = yii::$app->request->get('product');
        $where['group'] = yii::$app->request->get('group') ? yii::$app->request->get('group') : "";
        $where['bank'] = yii::$app->request->get('bank');
        $where['type_child'] = yii::$app->request->get('type_child');
        $where['purchase_num'] = trim(yii::$app->request->get('purchase_num'));
        $model = new Bank_bill();
        $list = $model->get_bank_bill_list($p, $s, $where);
        $size = $model->get_bank_bill_size($where);
        $service = new PayService();
        $list = $service->validateBankCustomer($list);
        echo json_encode(['list' => $list, 'total' => $size]);
    }

    public function actionCommon()
    {
        $model = new Bank_bill();
        $bank_type = $model->get_bank_type();
        array_push($bank_type, ['id' => 0, 'name' => '请选择']);
        $departmentModal = new Department();
        $group_list = $departmentModal->all();
        foreach ($group_list as $k => $v) {
            $group_list[$k]['name'] = $v['group_name'];
        }
        //业务员列表
        $saleman_model = new Staff();
        $saleman_list = $saleman_model->get_all_saleman();
        $bank = yii::$app->params['new_bank'];
        return json_encode(['status' => 1, 'data' => [
            'bankType' => $bank_type,
            'bank' => $bank,
            'group' => $group_list,
            'staff' => $saleman_list
        ]]);
    }

    public function actionSumEveryMonth()
    {
        $year = yii::$app->request->get('year', date('Y'));
        $model = new Bank_bill();
        $list = $model->sumMoneyEveryMonth($year);
        foreach ($list as $k => $v) {
            $list[$k]['money'] = (float)$v['money'];
        }
        return \json_encode(['list' => $list]);
    }

    public function actionSumMonth()
    {
        $month = yii::$app->request->get('month', date('Y-m'));
        $model = new Bank_bill();
        $list = $model->sumMoneyByMonth($month);
        $type = $model->get_bank_type();
        foreach ($list as $k => $v) {
            foreach ($type as $v2) {
                if ($v2['id'] == $v['type']) {
                    $list[$k]['type_name'] = $v2['name'];
                }
            }
            $list[$k]['money'] = (float)$v['money'];
        }
        usort($list, function ($a, $b) {
            if ($a > $b) {
                return 1;
            }
            return 0;
        });
        return json_encode(['list' => $list]);
    }

    public function actionSplitBank()
    {
        if (!yii::$app->request->isPost) {
            return json_encode(['code' => 0, 'msg' => '非法登录']);
        }
        $id = yii::$app->request->post('id');
        $invoice = yii::$app->request->post('invocie_num');
        $inMoney = yii::$app->request->post('in_money');
        $outMoney = yii::$app->request->post('out_money');
        //复制一条记录
        $model = new Bank_bill();
        $info = $model->one(['id' => $id]);
        if ($info['pid'] != $info['id']) {
            return json_encode(['code' => 0, 'msg' => '当前记录已经是拆分项了，无法继续拆分']);
        }
        $info['pid'] = $info['id'];
        $info['invocie_num'] = $invoice;
        $info['in_money'] = $inMoney;
        $info['out_money'] = $outMoney;
        unset($info['id']);
        //插入一条数据
        try {
            $re = $model->insert($info);
            if ($re) {
                $model->validateBankMoeny($info['pid']);
                return json_encode(['code' => 1, 'msg' => '拆分成功']);
            } else {
                return json_encode(['code' => 0, 'msg' => '添加失败,请重试']);
            }
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            return json_encode(['code' => 0, 'msg' => $msg]);
        }

    }
}